package controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.PaperLog;

import core.CurrentUser;
import core.PgSQL;
import core.View;

public class UserFootprint extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static PgSQL db = PgSQL.getInstance();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CurrentUser user = new CurrentUser(request);
		if(!user.isLoggedIn()){
			View.showMessage(request, response, "Login first, please.", "Login");
			return;
		}
		
		LinkedList<PaperLog> paperlogs = new LinkedList<PaperLog>();
		
		ResultSet rs = db.query("SELECT p.*,l.uid,u.username,l.score,l.dateline FROM qz_paperlog l LEFT JOIN qz_paper p ON p.pid=l.pid LEFT JOIN qz_user u ON u.uid=l.uid WHERE l.uid=" + user.getUid() + " ORDER BY l.dateline DESC");
		try{
			while(rs.next()){
				paperlogs.add(PaperLog.fromDatabase(rs));
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		request.setAttribute("paperlog[]", paperlogs.toArray(new PaperLog[0]));
		
		View.show(request, response, "user_footprint");
	}

}
